﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LogisticsBLL;
using Model;

namespace WebLosgic.Carress
{
    public partial class InsertCarrier : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["UserIDs"] != null)
                {
                    int UserID = Convert.ToInt32(Session["UserIDs"]);
                    UserNames.Text = Session["names"].ToString();
                    CarriessCyBLL ccb = new CarriessCyBLL();
                    int str = ccb.PDrole(UserID);
                    if (str == 0)
                    {
                        Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('只有承运业务员和系统管理员才可以添加承运单');location.href='../index.aspx';", true);
                    }
                    
                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(GetType(),"", "alert('请先去登录'); location.href='../login.aspx';", true);
                }
                
            }
            
        }


        protected void BtninstCheng_Click(object sender, EventArgs e)
        {
            int UserID = Convert.ToInt32(Session["UserIDs"]);
            Carriers cs = new Carriers();
            cs.SendCompany = TxtSendCompany.Text;
            cs.SendAddress = TxtSendAddress.Text;
            cs.SendLinkman = TxtSendLinkman.Text;
            cs.SendPhone = TxtSendPhone.Text;
            cs.ReceiveCompany = TexReceiveCompany.Text;
            cs.ReceiveAddress = TxtFK_ReceiveAddress.Text;
            cs.ReceiveLinkman = TexReceiveLinkman.Text;
            cs.ReceivePhone = TxtReceivePhone.Text;
            cs.LeaverDate = TxtLeaverDate.Text;
            cs.InsuranceCost = TexInsuranceCost.Text;
            cs.TransportCost = TexTransportCost.Text;
            cs.OtherCost = TxtOtherCost.Text;
            int total = jisuan();
            cs.TotalCost = total.ToString();
            cs.Remark = TxtRemark.Text;
            cs.UserID = UserID;
            CarriessCyBLL cb = new CarriessCyBLL();
            string str = cb.InsertCarries(cs);
            string name = Session["names"].ToString();
            string Behavor = "" + name + ": 添加承运单";
            int TypeId = 14;
            string Parameters = @"" + TxtLeaverDate.Text + "," + TxtSendCompany.Text + "," + TexReceiveCompany.Text;
            string ProcName = "insert into Carriers values(@SendCompany)";
            HttpRequest request = HttpContext.Current.Request;
            string IP = "192.168.0.1";
            int IsExcePion = 0;
            if (str== "添加成功！")
            {
                IsExcePion = 0;
                int a = ProcBLL.InserSys(Behavor, TypeId, UserID, Parameters, ProcName, IP, IsExcePion);
                if (a > 0)
                {
                    Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('" + str + "');location.href='CarrierManage.aspx';", true);
                }
                
            }
            else
            {
                IsExcePion = 1;
                int a = ProcBLL.InserSys(Behavor, TypeId, UserID, Parameters, ProcName, IP, IsExcePion);
                if (a > 0)
                {
                    Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('" + str + "')", true);
                }
            }
            

        }


        public int jisuan()
        {
            string a = TexInsuranceCost.Text;
            string b = TexTransportCost.Text;
            string c = TxtOtherCost.Text;
            int sum = 0;
            if (a == "")
            {
                a = "0";
               
            }
            if (b == "")
            {
               b = "0";
            }
            if (c == "")
            {
               c = "0";
            }
            sum = Convert.ToInt32(a) + Convert.ToInt32(b) + Convert.ToInt32(c);
            return sum;
        }

        protected void BtnReturn_Click(object sender, EventArgs e)
        {
            TxtSendCompany.Text="";
            TxtSendAddress.Text="";
            TxtSendLinkman.Text="";
            TxtSendPhone.Text="";
            TexReceiveCompany.Text="";
            TxtFK_ReceiveAddress.Text="";
            TexReceiveLinkman.Text="";
            TxtReceivePhone.Text="";
            TxtLeaverDate.Text="";
            TexInsuranceCost.Text="";
            TexTransportCost.Text="";
            TxtOtherCost.Text="";
        }
    }
}